// -*- coding: utf-8 -*- 
/**
 * Project: Java8InActionT
 *
 * @author: yanking
 * Create time: 2022-03-07 22:20
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.WordCounter;

import org.junit.jupiter.api.Test;

public class WordCounter {
    private final int counter;
    private final boolean lastSpace;

    public WordCounter(int counter, boolean lastSpace) {
        this.counter = counter;
        this.lastSpace = lastSpace;
    }

    /**
     * 方法用来记录单词数量，counter是当前分解迭代器计算得到的单词数量，使用lastSpace判断当前是否是最后一个空格
     * eg: __ 出现了两个连续的空格，第一个字符的lastSpace，true而第二个的是false,通过这个方法避免了多个连续空格的干扰
     *
     * @param c
     * @return
     */
    public WordCounter accumulate(Character c) {
        if (Character.isWhitespace(c)) {
            return lastSpace ? this : new WordCounter(counter, true);
        } else {
            return lastSpace ? new WordCounter(counter + 1, false) : this;
        }
    }

    /**
     * 数据合并方法，并行运行的时候，使用combine方法将各个运算的结果合并在一起
     *
     * @param wordCounter
     * @return
     */
    public WordCounter combine(WordCounter wordCounter) {
        return new WordCounter(counter + wordCounter.counter, wordCounter.lastSpace);
    }

    public int getCounter() {
        return counter;
    }


    @Test
    public void shout() {

    }

}
